import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import queryCampusList, { updateSmsAgentStatus, updateUserPickUpStatus } from './service';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
        updateSmsAgentStatus: Effect;
        updateUserPickUpStatus: Effect;
    };
    reducers: {
        save: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'campusCommunityList',

    state: {
        dataInfo: {
            list: [],
            count: 0,
        },
    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(queryCampusList, payload);
            yield put({
                type: 'save',
                payload: response.body,
            });
        },
        * updateSmsAgentStatus({ payload: { query, ...params } }, { call, put }) {
            const response = yield call(updateSmsAgentStatus, params);
            if (response.code === 0) {
                yield put({
                    type: 'fetch',
                    payload: { ...query },
                });
            }
        },
        * updateUserPickUpStatus({ payload: { query, ...params } }, { call, put }) {
            const response = yield call(updateUserPickUpStatus, params);
            if (response.code === 0) {
                yield put({
                    type: 'fetch',
                    payload: { ...query },
                });
            }
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                dataInfo: action.payload,
            };
        },
    },
};

export default Model;
